package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.service.*;
import com.wtwd.campus.utils.GetCurrentSysUserInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2021/06/08
 * @description
 */
@RestController
@RequestMapping("/kanban")
public class KanbanController {
    private static final Logger logger = LoggerFactory.getLogger(KanbanController.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;

    /**
     * 获取用户折线图
     *
     * @param type
     * @param year
     * @param request
     * @return
     */
    @GetMapping("/deviceIncreased/{type}/{year}")
    public RespEntity getDeviceIncreased(@PathVariable Integer type, @PathVariable Integer year, HttpServletRequest request) {
        logger.info("getDeviceIncreased : customerId = {},type = {},year = {}", type, year, request);
        //获取当前用户信息
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        List<Map<String, Object>> list = null;
        try {
            list = deviceInfoService.getDeviceIncreased(sysUser.getUserId(), type, year);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new RespEntity(RespCode.SUCCESS, list);
    }


    /**
     * 获取数据概况（包括学校数量,学生数量，校园卡数量，校园卡在线数量，校园卡离线数量，校园卡绑定数量，校园卡未绑定数量）
     *
     * @param
     * @return
     */
    @GetMapping("/getProfileData")
    public RespEntity getProfileData() {
        //获取当前用户信息
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        logger.info("getProfileData  param :" + sysUser);
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        //学校数量
        Integer schoolNum = schoolService.getSchoolCountByOrgCode(sysUser.getOrgCode());
        //学生数量
        Integer studentNum = studentService.getStudentCountByOrgCode(sysUser.getOrgCode());
        //获取设备相关信息的数量(包括设备在线数量，离线数量，校园卡绑定数量，未绑定数量,总数)
        Map<String, Object> restMap = deviceInfoService.getDeviceCountInfo(sysUser.getOrgCode());
        restMap.put("schoolNum", schoolNum);
        restMap.put("studentNum", studentNum);
        return new RespEntity(RespCode.SUCCESS, restMap);
    }

    /**
     * 测试加密
     *
     * @param id
     * @param request
     * @return
     */
    @DeleteMapping("/test/{id}")
    public RespEntity test(@PathVariable("id")Integer id, @RequestParam("param")String param, HttpServletRequest request) {
        System.out.println(id);
        System.out.println(param);
        System.out.println(request);
        System.out.println(request.getHeader("userId"));
        return new RespEntity(RespCode.SUCCESS);
    }

    @PostMapping("/testPost")
    public RespEntity testPost(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject);
        return new RespEntity(RespCode.SUCCESS);
    }
}
